<!--
 * @Author: your name
 * @Date: 2021-04-26 10:26:20
 * @LastEditTime: 2021-04-26 10:26:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \quanqudao_uniapp\App.vue
-->
<script>
import { getRedirectUri, passportCode } from "@/api/login";
import { sessionGet } from "@/api/wechat";

export default {
  onLaunch: async function (option) {
    let { platform, SDKVersion } = uni.getSystemInfoSync();
    uni.setStorageSync("SDKVersion", SDKVersion);
    uni.setStorageSync("platform", platform);
    // #ifdef WEB
    uni.setStorageSync("platform", "web");
    // #endif
    // #ifdef MP
    uni.setStorageSync("platform", "MP");
    // #endif

    if (
      option.path == "alipay-order/orderMeal/orderMeal" &&
      option.query.qrCode
    ) {
      this.$utils.setStorage("orderMealOption", option.query.qrCode);
    }

    if (option && option.query && option.query.order_no) {
      uni.setStorageSync("order_no", option.query.order_no);
    }

    // #ifdef H5
    if (option && option.query && option.query.scene) {
      const params = this.$utils.sceneDecode(option.query.scene);
      if (params.alliance && params.alliance === "1") {
        window.location.href = "/h5/#/other/downLoadAllianceApp/index";
        return;
      }
    }
    // #endif

    // #ifdef MP
    try {
      const updateManager = uni.getUpdateManager(); // 小程序版本更新管理器
      updateManager.onCheckForUpdate((res) => {
        // 检测新版本后的回调
        if (res.hasUpdate) {
          // 如果有新版本提醒并进行强制升级
          uni.showModal({
            content: "新版本已经准备好，是否重启应用？",
            showCancel: false,
            confirmText: "确定",
            success: (res) => {
              if (res.confirm) {
                updateManager.onUpdateReady((res) => {
                  // 新版本下载完成的回调
                  updateManager.applyUpdate(); // 强制当前小程序应用上新版本并重启
                });

                updateManager.onUpdateFailed((res) => {
                  // 新版本下载失败的回调
                  // 新版本下载失败，提示用户删除后通过冷启动重新打开
                  uni.showModal({
                    content: "下载失败，请删除当前小程序后重新打开",
                    showCancel: false,
                    confirmText: "知道了"
                  });
                });
              }
            }
          });
        }
      });
    } catch (error) {}
    // #endif
    // #ifdef WEB
    try {
      // 此处处理微信浏览器下注册过用户可以无需重复登录逻辑
      if (
        this.$utils.isWechat() &&
        !this.$utils.getStorage("hasUri") &&
        !this.$utils.getStorage("token")
      ) {
        if (window.location.href.indexOf("?code") !== -1) {
          let params = this.$utils.oneValues(window.location.href);
          passportCode({
            code: params.code
          }).then((res) => {
            if (!res.code && res.data.access_token)
              this.$utils.setStorage("token", res.data.access_token);
          });
          return;
        }
        const res = await getRedirectUri();
        if (!res.code && res.data.uri) {
          window.location.href = res.data.uri;
        }
      }
    } catch (error) {}
    // #endif
  },
  onShow: async function (option) {
    if (this.$store.getters.has_is_one_store) {
      let query_scene = this.$utils.sceneDecode(option.query.scene);
      let q_store_id =
        query_scene.store_id > 0 ? query_scene.store_id : query_scene.store;
      if (q_store_id && parseInt(q_store_id)) {
        this.$store.commit("setStoreId", q_store_id);
        this.getDefultStore();
      }

      if (option.path == "face-pay/face-pay/payOut") {
        if (option.query.qrCode || option.query.q) {
          //#ifdef  MP-WEIXIN
          const url = decodeURIComponent(option.query.q);
          // #endif
          // #ifdef MP-ALIPAY
          const url = decodeURIComponent(option.query.qrCode);
          // #endif
          const params = this.$utils.oneValues(url);
          if (params.id) {
            const aggData =
              await this.$allrequest.request.getStoreIdByAggregate({
                id: params.id
              });
            if (aggData.data.store_id && aggData.data.store_id > 0) {
              this.$store.commit("setStoreId", aggData.data.store_id);
              this.getDefultStore();
            }
          }
        } else if (option.query.scene) {
          const params = this.$utils.sceneDecode(option.query.scene);
          if (params.store_id && params.store_id > 0) {
            this.$store.commit("setStoreId", params.store_id);
            this.getDefultStore();
          }
        }
      }
    }
    if (option.query.scene) {
      const params = this.$utils.sceneDecode(option.query.scene);
	   if(params.mch_id){
		   console.log('我走了入驻商',params)
		   uni.setStorageSync("mch_id",params.mch_id);
	   }
	   uni.setStorageSync("scene", option.scene);
      if (params.store_id && params.store_id > 0) {
        this.$store.commit("setStoreId", params.store_id);
        this.newgetDefultStore(params.mid);
      }
      if (params.mid && params.mid > 0) {
        //切换门店
        this.newgetDefultStore(params.mid);
      }
    }

    // 平台当面付 获取参数
    if (option.path == "face-pay/face-pay/pay" && option.query.q) {
      const url = decodeURIComponent(option.query.q).split("/");
      if (url[url.length - 1]) {
        this.$store.commit("setStoreId", url[url.length - 1]);
      }
    }

    this.bindParent(option);

    // 复制抖音连接
    // #ifndef MP-TOUTIAO
    this.$allrequest.publicMethod.defaultStore().then((res) => {
      if (!res.data.store.douyin_url) return;

      uni.setClipboardData({
        data: res.data.store.douyin_url,
        success() {
          uni.hideToast();
        }
      });
    });
    // #endif

    // #ifdef MP-WEIXIN
    try {
      uni.setStorageSync("scene", option.scene);

      if (
        [1242, 1243, 1245].includes(option.scene) &&
        option.query?.session_key
      ) {
        const getUseInfoRes = await sessionGet({
          session_key: option.query.session_key
        });

        if (!getUseInfoRes.code && !getUseInfoRes.data.errcode) {
          let { session } = getUseInfoRes.data;
          uni.setStorageSync("session_addr", session);

          let mdId = session.out_store_id.split("_")[2];
          this.$allrequest.shop
            .getMdList({
              md_id: mdId,
              mdId
            })
            .then((res) => {
              if (!res.code) {
                uni.setStorageSync(
                  "storeMdData",
                  JSON.stringify(res.data.list[0])
                );
                uni.setStorageSync("MdDataReload", "true");
              }
            });
        }
      }
    } catch (error) {}
    // #endif
  },
  onHide: function () {},
  methods: {
    newgetDefultStore(option) {
      this.$allrequest.publicMethod
        .newdefaultStore({
          md_id: option
        })
        .then((res) => {
          if (res.code == 0) {
            const jsonString = JSON.stringify(res.data);
            uni.setStorageSync("storeMdData", jsonString);
            // this.$store.commit("setStores", res.data.store);
            // this.$store.commit("setImages", res.data.images);
            // this.$store.commit("setShareSetting", res.data.share_setting);
            // this.$store.commit(
            //   "setOneStoreSwitchStore",
            //   res.data.one_store_switch_store
            // );
          }
        });
    },
    getDefultStore(option) {
      this.$allrequest.publicMethod.defaultStore({}).then((res) => {
        if (res.code == 0) {
          this.$store.commit("setStores", res.data.store);
          this.$store.commit("setImages", res.data.images);
          this.$store.commit("setShareSetting", res.data.share_setting);
          this.$store.commit(
            "setOneStoreSwitchStore",
            res.data.one_store_switch_store
          );
        }
      });
    },
    bindParent(option) {
      let share_user_id = 0,
        page_type = 2;
      if (option.query && option.query.user_id) {
        share_user_id = option.query.user_id;
        page_type = option.query.page_type;
      }
      // #ifdef H5
      if (option.query && parseInt(option.query.is_login)) {
        this.$utils.toUrl("/other/downLoadApp/index", "redirectTo");
      }
      // #endif

      if (option.query && option.query.scene) {
        const scene = this.$utils.sceneDecode(option.query.scene);
        if (scene.uid) {
          share_user_id = scene.uid;
        }
        if (scene.u) {
          share_user_id = scene.u;
        }

        if (scene.page_type) page_type = scene.page_type;

        if (this.$store.getters.has_is_one_store) {
          let scene_store = scene.store > 0 ? scene.store : scene.store_id;
          if (scene_store && parseInt(scene_store) > 0) {
            uni.setStorageSync("store_id", scene_store);
            this.getDefultStore();
          }
          let scene_s = scene.s > 0 ? scene.s : scene.store_id;
          if (scene.s && parseInt(scene.s) > 0) {
            uni.setStorageSync("store_id", scene.s);
            this.getDefultStore();
          }
        }
      }
      if (share_user_id && share_user_id > 0) {
        let userInfo = uni.getStorageSync("userInfo");
        if (userInfo) {
          // 绑定上下级
          this.$allrequest.share.bindParent({
            parent_id: share_user_id,
            condition: 0,
            page_type
          });
        } else {
          if (share_user_id != userInfo.id)
            // 记录一下
            uni.setStorageSync("parent_user_id", share_user_id);
          uni.setStorageSync("page_type", page_type);
        }
      }
    }
  }
};
</script>

<style lang="scss">
/*每个页面公共css */
@import "@/uview-ui/index.scss";
@import "./static/css/global.css";
@import "./static/iconfont/diyIcon.css";

/deep/.uni-scroll-view::-webkit-scrollbar {
  /* 隐藏滚动条，但依旧具备可以滚动的功能 */
  display: none;
}
.footNav {
  width: 100%;
  height: calc(env(safe-area-inset-bottom) + 100rpx);
  min-height: 100rpx;
  background-color: #fff;
  z-index: 999;
}
.footNav.fixed {
  position: fixed;
  bottom: 0;
  left: 0;
}
.fle-content {
  display: flex;
  flex-direction: column;
}

.u-numberbox {
  height: 6.4vw;
  border: 1px solid #f2f2f2;
  border-radius: 1.334vw;
  overflow: hidden;
  display: flex;
  align-items: center;
  .u-icon-plus {
    background: none !important;
    position: relative;
    height: 4.26vw !important;
    border-left: 1px solid #f2f2f2;
  }
  .u-icon-minus {
    background: none !important;
    position: relative;
  }

  .u-number-input::before {
    content: "";
    width: 1px;
    height: 4.26vw;
    background: #f2f2f2;
    border-radius: 0px;
    left: -2.13vw;
    top: 1.07vw;
  }
  .u-number-input {
    background: none !important;
  }
}
// 优惠券样式
.collapse {
  .u-collapse-item {
    /deep/.u-collapse-head {
      justify-content: flex-start;
      line-height: 48rpx;
      padding: 0;
      color: #8c8c8c;
    }
    /deep/.u-collapse-title {
      flex: initial;
      font-size: 24rpx;
      color: #8c8c8c;
    }
    /deep/.u-collapse-content {
      font-size: 24rpx;
      color: #8c8c8c;
    }
  }
}

::-webkit-scrollbar {
  display: none;
  width: 0 !important;
  height: 0 !important;
  -webkit-appearance: none;
  background: transparent;
}
</style>
